package com.tengfei.adapterdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.tengfei.adapterdemo.adapter.MyListViewAdapter;
import com.tengfei.adapterdemo.entry.ReceiveData;
import com.tengfei.adapterdemo.util.HttpUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final String URL = "https://api.apiopen.top/getImages?count=30";

    private ListView mLv;
    private Button mBtn;
    private SwipeRefreshLayout mSwipeRefresh;

    private MyListViewAdapter myListViewAdapter;
    private List<ReceiveData.PhotoData> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        btnaction();
        netstart();
        listener();

    }


    private void initview() {
        mLv = findViewById(R.id.my_lv_1);
        mBtn = findViewById(R.id.page_1);
        mSwipeRefresh = findViewById(R.id.swiprefresh);
        // 设置状态栏标题
        getSupportActionBar().setTitle("自定义adapter下普通加载图片(下拉刷新)");
    }

    private void btnaction() {
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ImageFrameworkActivity.class);
                startActivity(intent);
            }
        });
    }

    private void netstart() {
        // 执行网络请求线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtil.doPost(URL, "");
                Message message = new Message();
                message.what = 1;
                message.obj = result;
                mHandler.sendMessage(message);
            }
        }).start();
    }

    private void listener() {
        // 设置手势滑动监听
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 执行一个网络请求线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String result = HttpUtil.doPost(URL, "");
                        // 发送一个handler消息
                        Message message = new Message();
                        message.what = 1;
                        message.obj = result;
                        mHandler.sendMessage(message);
                    }
                }).start();

            }
        });
    }


    // 线程异步消息传递
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            ReceiveData receiveData = new Gson().fromJson(result, ReceiveData.class);
            // 刷新时先清理
            if (msg.what == 2) {
                datas.clear();
            }
            datas.addAll(receiveData.getResult());
            mSwipeRefresh.setRefreshing(false);
            // 刷新成功，关闭刷新
            int screenWidth = MainActivity.this.getWindowManager().getDefaultDisplay().getWidth();
            myListViewAdapter = new MyListViewAdapter(datas, screenWidth);
            mLv.setAdapter(myListViewAdapter);
        }

    };
}